/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_CRC_H__
#define __PONY_CRC_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
#include <stddef.h>   // size_t
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

/**
 *  +=======================+=======================+
 *  | algorithm             | crc                   |
 *  +=======================+=======================+
 *  | crc8                  | 0xda                  |
 *  | crc16                 | 0x5fc7                |
 *  | crc16_modbus          | 0x7bc7                |
 *  | crc_ccitt             | 0xb674                |
 *  | crc_xmodem            | 0x32b4                |
 *  | crc_ccitt_pcp         | 0xef6f                |
 *  | crc32                 | 0xdb577c2c            |
 *  | crc64_ecma            | 0xdc47cef32ed960d8    |
 *  | crc64_we              | 0x0ebe168b82b8c5f7    |
 *  +-----------------------+-----------------------+
 */
#define CRC_TEST_DATA "pony"

/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

/* crc-8 */
uint8_t crc8_calc(const void *data, size_t len);
void crc8_gen_table(uint8_t table[256]);

/* crc-16 */
uint16_t crc16_calc(const void *data, size_t len);
uint16_t crc16_modbus(const void *data, size_t len);
uint16_t crc16_update(uint16_t crc, char ch);
void crc16_gen_table(uint16_t table[256]);

/* crc-ccitt (16 bit) */
uint16_t crc_ccitt(const void *data, size_t len);
uint16_t crc_xmodem(const void *data, size_t len);
uint16_t crc_ccitt_pcp(const void *data, size_t len);
uint16_t crc_ccitt_update(uint16_t crc, char ch);
void crc_ccitt_gen_table(uint16_t table[256]);

/* crc-32 */
uint32_t crc32_calc(const char *data, size_t len);
uint32_t crc32_update(uint16_t crc, char ch);
void crc32_gen_table(uint32_t table[256]);

/* crc-64 */
uint64_t crc64_ecma(const char *data, size_t len);
uint64_t crc64_we(const char *data, size_t len);
void crc64_gen_table(uint64_t table[256]);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_CRC_H__ */
